<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<head>
<script type="text/javascript">
	debugger;
	var doctorId = '${doctorRegistration.doctorId}';
	var registrations = [];
	registrations = '${doctorRegistration.registrations}';
	
	registrations = $.parseJSON(registrations);
	
	var pageRegistrations = [];
	for(var i=0;i<registrations.length;i++) {
		var registration = {
			'timestamp': registrations[i].timeSegment,
			'timeSegment': dateToString(registrations[i].timeSegment),
			'reserveRegisterFreeNum':registrations[i].reserveRegisterFreeNum,
			'reserveRegisteredNum':registrations[i].reserveRegisteredNum,
			'siteRegisterFreeNum':registrations[i].siteRegisterFreeNum,
			'siteRegisteredNum':registrations[i].siteRegisteredNum
		};
		pageRegistrations.push(registration);
		
	}
	debugger;
	
	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			striped : true,
			rownumbers : true,
			singleSelect : true,
			idField : 'doctorId',
			sortName : 'id',
			sortOrder : 'asc',
			columns :[[{
				width : '200',
				title : '时间段',
				field : 'timeSegment'
			}, {
				width : '200',
				title : '时间戳',
				field : 'timestamp',
				hidden: true
			},{
				width : '150',
				title : '（预约挂号）剩余号数',
				field : 'reserveRegisterFreeNum'
			}, {
				width : '150',
				title : '（预约挂号）已挂号数',
				field : 'reserveRegisteredNum'
			},{
				width : '150',
				title : '（现场挂号）剩余号数',
				field : 'siteRegisterFreeNum'
			}, {
				width : '150',
				title : '（现场挂号）已挂号数',
				field : 'siteRegisteredNum'
			} ] ],
			data: pageRegistrations,
			toolbar : '#toolbar'
		});
	});
	function reserveRegister() {
		debugger;
		var rows = dataGrid.datagrid('getSelections');
		if(rows.length != 0) {
			if(rows[0].reserveRegisterFreeNum <= 0) {
				$.messager.alert('错误', "该时间段剩余预约挂号数为0", 'error');
			} else {
				$.ajax({
					type: "POST",
					async: false,
					data:  {
						'doctorId':doctorId,
						'timestamp':rows[0].timestamp
					},
			        url: '${ctx}/reserveRegister/add',
			        dataType: "json",
			        success:function(result) {
			        	if(result.status.errorCode == 0) {
			        		debugger;
			        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
			        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/reserveRegister/addPage?doctorId=' + doctorId);
			        		});
			        	} else {
			        		$.messager.alert('错误', result.status.errorMsg, 'error');
			        	}
			        },
			        error:function(result) {
			        }
		   	 	});
			}
			
			
		} else {
			$.messager.alert('错误', "您未选择预约时间", 'error');
		}
	}
	
	function dateToString(timestamp) {
		datetime = new Date(timestamp);
		var year = datetime.getFullYear();
		var month = datetime.getMonth()+1;
		var date = datetime.getDate();
		var day = datetime.getDay();
		var hour = datetime.getHours();
		
		var s = year + '/' + month + '/' + date;
		if(hour == 8) {
			s += ' 早上';
		} else {
			s += ' 下午';
		}
		s += '（星期' + day + '）';
		return s;
	}
</script>
</head>

<div class="easyui-layout" data-options="fit:true,border:false">
	
	<div data-options="region:'center',fit:true,border:false">
		<table id="dataGrid" data-options="fit:true,border:false"></table>
	</div>
	
	<div id="toolbar" style="display: none;">
		<c:if test="${fn:contains(sessionInfo.resourceList, '/reserveRegister/add')}">
			<a onclick="reserveRegister();" href="javascript:void(0);"
				class="easyui-linkbutton"
				data-options="plain:true,iconCls:'icon_add'">预约</a>
		</c:if>
	</div>
</div>